<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('提醒管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>手机号码：</label>
                                <input type="text" name="mobilePhone"/>
                            </li>
                            <li>
                                <label>用户姓名：</label>
                                <input type="text" name="userName"/>
                            </li>
                            <li>
                                <label>业务名称：</label>
                                <input type="text" name="businessName"/>
                            </li>
                            <li>
                                <label>业务类型：</label>
                                <input type="text" name="businessType"/>
                            </li>
                            <li>
                                <label>记录类型：</label>
                                <select name="recordType">
                                    <option value="">所有</option>
                                    <option value="0">任务</option>
                                    <option value="1">记录</option>
                                </select>
                            </li>
                            <li>
                                <label>状态：</label>
                                <select name="status">
                                    <option value="">所有</option>
                                    <option value="0">待发送</option>
                                    <option value="1">已发送</option>
                                    <option value="2">发送失败</option>
                                    <option value="3">已取消</option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="business:reminder:add">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="business:reminder:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="business:reminder:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-info" onclick="$.table.importExcel()" shiro:hasPermission="business:reminder:import">
                    <i class="fa fa-upload"></i> 导入
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="business:reminder:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <!-- 导入区域 -->
     <script th:inline="javascript">
         var editFlag = [[${@permission.hasPermi('business:reminder:edit')}]];
         var removeFlag = [[${@permission.hasPermi('business:reminder:remove')}]];
         var detailFlag = [[${@permission.hasPermi('business:reminder:detail')}]];
         var triggerFlag = [[${@permission.hasPermi('business:reminder:trigger')}]];
         var cancelFlag = [[${@permission.hasPermi('business:reminder:cancel')}]];
         var reminderTypeData = [
             {code: "1", text: "到期提醒"},
             {code: "2", text: "退订提醒"},
             {code: "3", text: "续费提醒"}
         ];
         var channelData = [
             {code: "0", text: "企业微信"},
             {code: "1", text: "短信"},
             {code: "2", text: "短信和企业微信"}
         ];
         var recordTypeData = [
             {code: "0", text: "任务"},
             {code: "1", text: "记录"}
         ];
         var sourceTypeData = [
             {code: "0", text: "系统自动"},
             {code: "1", text: "手工录入"},
             {code: "2", text: "Excel导入"}
         ];
         var statusData = [
             {code: "0", text: "待发送"},
             {code: "1", text: "已发送"},
             {code: "2", text: "发送失败"},
             {code: "3", text: "已取消"}
         ];

         var prefix = ctx + "business/reminder";

         $(function() {
             var options = {
                 url: prefix + "/list",
                 createUrl: prefix + "/add",
                 updateUrl: prefix + "/edit/{id}",
                 removeUrl: prefix + "/remove",
                 exportUrl: prefix + "/export",
                 modalName: "提醒",
                 columns: [{
                     checkbox: true
                 },
                 {
                     field: 'reminderId',
                     title: 'ID'
                 },
                 {
                     field: 'mobilePhone',
                     title: '手机号码'
                 },
                 {
                     field: 'userName',
                     title: '用户姓名'
                 },
                 {
                     field: 'businessName',
                     title: '业务名称'
                 },
                 {
                     field: 'reminderType',
                     title: '提醒类型',
                     formatter: function(value, row, index) {
                         for(var i=0; i<reminderTypeData.length; i++) {
                             if(value == reminderTypeData[i].code) {
                                 return reminderTypeData[i].text;
                             }
                         }
                         return value;
                     }
                 },
                 {
                     field: 'endTime',
                     title: '到期时间',
                     formatter: function(value, row, index) {
                         if (!value) return ''; // 空值处理
                         const date = new Date(value);
                         const pad = num => (num < 10 ? '0' + num : num);
                         const formatted = `${date.getFullYear()}-${pad(date.getMonth()+1)}}`;
                         return '<span title="' + value + '">' + formatted + '</span>';
                     }
                 },
                 {
                     field: 'status',
                     title: '状态',
                     formatter: function(value, row, index) {
                         for(var i=0; i<statusData.length; i++) {
                             if(value == statusData[i].code) {
                                 var statusClass = "";
                                 if(value == "0") statusClass = "primary"; // 蓝色
                                 else if(value == "1") statusClass = "success"; // 绿色
                                 else if(value == "2") statusClass = "danger"; // 红色
                                 else if(value == "3") statusClass = "warning"; // 黄色

                                 return '<span class="label label-'+statusClass+'">'+statusData[i].text+'</span>';
                             }
                         }
                         return value;
                     }
                 },
                 {
                     field: 'channel',
                     title: '提醒渠道',
                     formatter: function(value, row, index) {
                         for(var i=0; i<channelData.length; i++) {
                             if(value == channelData[i].code) {
                                 return channelData[i].text;
                             }
                         }
                         return value;
                     }
                 },
                 {
                     field: 'content',
                     title: '提醒内容',
                     formatter: function(value, row, index) {
                         if (value && value.length > 20) {
                             return '<span title="' + value + '">' + value.substring(0, 20) + '...</span>';
                         }
                         return value;
                     }
                 },
                 {
                     field: 'remark',
                     title: '备注',
                     formatter: function(value, row, index) {
                         if (value && value.length > 20) {
                             return '<span title="' + value + '">' + value.substring(0, 20) + '...</span>';
                         }
                         return value;
                     }
                 },
                 {
                     field: 'planSendTime',
                     title: '计划发送时间'
                 },
                 {
                     field: 'sendTime',
                     title: '实际发送时间'
                 },
                 {
                     title: '操作',
                     align: 'center',
                     formatter: function(value, row, index) {
                         var actions = [];
                         if (row.recordType == '0' && row.status == '0') {
                             if (triggerFlag) {
                                 actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="triggerSend(\'' + row.reminderId + '\')"><i class="fa fa-send"></i> 发送</a> ');
                             }
                             if (cancelFlag) {
                                 actions.push('<a class="btn btn-warning btn-xs" href="javascript:void(0)" onclick="cancel(\'' + row.reminderId + '\')"><i class="fa fa-ban"></i> 取消</a> ');
                             }
                         }

                         // 始终显示编辑按钮
                         if (editFlag) {
                             actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.reminderId + '\')"><i class="fa fa-edit"></i> 编辑</a> ');
                         }

                         if (detailFlag) {
                             actions.push('<a class="btn btn-info btn-xs" href="javascript:void(0)" onclick="$.operate.detail(\'' + row.reminderId + '\')"><i class="fa fa-search"></i> 详情</a> ');
                         }

                         // 添加发送信息提示按钮
                         actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="sendInfoMsg(\'' + row.reminderId + '\', \'' + row.mobilePhone + '\', \'' + (row.content || '') + '\')"><i class="fa fa-commenting"></i> 企业微信</a> ');
                         actions.push('<a class="btn btn-primary btn-xs" href="javascript:void(0)" onclick="sendMobile(\'' + row.reminderId + '\', \'' + row.mobilePhone + '\', \'' + (row.content || '') + '\')"><i class="fa fa-commenting"></i> 短信</a> ');

                         // 始终显示删除按钮
                         if (removeFlag) {
                             actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.reminderId + '\')"><i class="fa fa-trash"></i> 删除</a>');
                         }

                         return actions.join('');
                     }
                 }]
             };
             $.table.init(options);
         });

         /* 手动触发发送提醒 */
         function triggerSend(reminderId) {
             $.modal.confirm("确认要发送该提醒吗？", function() {
                 var url = prefix + "/trigger/" + reminderId;
                 $.operate.post(url);
             });
         }

         /* 取消提醒 */
         function cancel(reminderId) {
             $.modal.confirm("确认要取消该提醒吗？", function() {
                 var url = prefix + "/cancel/" + reminderId;
                 $.operate.post(url);
             });
         }

         /* 发送企业微信消息提示 */
         function sendInfoMsg(reminderId, mobilePhone, content) {
             if ($.common.isEmpty(content)) {
                 $.modal.alertWarning("提醒内容为空，无法发送");
                 return;
             }

             // 确认提示
             $.modal.confirm("确认向 " + mobilePhone + " 发送提示信息吗？", function() {
                 // 发送提示信息
                 $.operate.post(prefix + "/sendInfoMsg", {
                     "reminderId": reminderId,
                     "mobilePhone": mobilePhone,
                     "content": content
                 });
             });
         }

         /* 创建消息任务 */
         function createMessage(reminderId) {
             $.modal.confirm("确认要创建消息发送任务吗？", function() {
                 var url = ctx + "business/message/createFromReminder/" + reminderId;
                 $.operate.post(url, null, null, function(result) {
                     if (result.code == web_status.SUCCESS) {
                         $.modal.alertSuccess("创建消息任务成功，可以到消息管理页面查看并发送");
                     } else {
                         $.modal.alertError(result.msg);
                     }
                 });
             });
         }

         /* 发送短信提示 */
         function sendMobile(reminderId, mobilePhone, content) {
             if ($.common.isEmpty(content)) {
                 $.modal.alertWarning("提醒内容为空，无法发送");
                 return;
             }

             // 确认提示
             $.modal.confirm("确认向 " + mobilePhone + " 发送提示信息吗？", function() {
                 // 发送提示信息
                 $.operate.post(prefix + "/sendMobile", {
                     "reminderId": reminderId,
                     "mobilePhone": mobilePhone,
                     "content": content
                 });
             });
         }

     </script>
     <script id="importTpl" type="text/template">
    <form enctype="multipart/form-data" class="mt20 mb10">
        <div class="col-xs-offset-1">
            <input type="file" id="file" name="file"/>
            <div class="mt10 pt5">
                <input type="checkbox" id="updateSupport" name="updateSupport" title="如果提醒任务已经存在，更新这条数据。"> 是否更新已经存在的提醒数据
                 &nbsp;    <a onclick="$.table.importTemplate()" class="btn btn-default btn-xs"><i class="fa fa-file-excel-o"></i> 下载模板</a>
            </div>
            <font color="red" class="pull-left mt10">
                提示：仅允许导入"xls"或"xlsx"格式文件！
            </font>
        </div>
    </form>
    </script>
</body>
</html> 